package javase.javase02.networkprograming;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

/**
 * @author: Gan
 * @date 2021/4/23 0023 11:43
 * @description:
 **/
public class Day22 {
    public static void main(String[] args) throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket(3301);
        while (true) {
            System.out.println("输入：");
            // 来自应用层的数据
            Scanner scan = new Scanner(System.in);
            String s = scan.next();

            // 建立udp的socket对象

            // 将要发送的数据封装成数据包
            // DatagramPacket(byte[] buf,  int offset,int length, InetAddress address, int port)

            byte[] bytes =(s).getBytes();
            InetAddress targetIP = InetAddress.getByName("192.168.3.130");
            int port = 11111;
            DatagramPacket packet =
                    new DatagramPacket(bytes, 0, bytes.length, targetIP, port);

            // send(DatagramPacket p)
            // 从此套接字发送数据报包。
            datagramSocket.send(packet);
        }
        // 释放资源
        //datagramSocket.close();
    }
}
